package tts.controller.groups;

import org.springframework.beans.factory.annotation.Autowired;

import tts.controller.RedirectAction;
import tts.dao.GroupsHome;
import tts.model.Groups;
import tts.model.Users;

public class Delete extends RedirectAction{

	private static final long serialVersionUID = 7628675327304635055L;
	@Autowired
	private GroupsHome groupsHome;
	private int groups_id;

	//fields
	public int getGroups_id() {
		return groups_id;
	}
	public void setGroups_id(int groups_id) {
		this.groups_id = groups_id;
	}

	public String delete() {
		try {
			Groups group = groupsHome.findById(this.groups_id);
			for (Users u : group.getUsersList())
				u.getGroupsList().remove(group);
			groupsHome.remove(group);
		} catch (RuntimeException e) {
			addActionError(e.getMessage());
			return ERROR;
		}
		addActionMessage(getText("group.deleted"));
		return SUCCESS;
	}
}